﻿using AutoMapper;
using B.Rabbit.DataBase.Domain;
using B.Rabbit.DataBase.ErrorCode;
using B.Rabbit.DataBase.Infrastructure.Infrastructure;
using B.Rabbit.DataBase.Read.Api.Applocation.Command.Rabbit.Health;
using MediatR;

namespace B.Rabbit.DataBase.Read.Api.Applocation.Handler.Rabbit.Health
{
    public class UpdateHealthCommandHandler : IRequestHandler<UpdateHealthCommand, ApiResult<int>>
    {
        IBaseRepository<HealthCareModel> healthmodel;
        IMapper mapper;

        public UpdateHealthCommandHandler(IBaseRepository<HealthCareModel> healthmodel, IMapper mapper)
        {
            this.healthmodel = healthmodel;
            this.mapper = mapper;
        }
        /// <summary>
        /// 更新保健信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(UpdateHealthCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var health = await healthmodel.GetValue(request.Id);
            var entity = mapper.Map(request, health);
            var r = await healthmodel.UpdateAsync(entity);
            if (r > 0)
            {
                res.msg = "修改成功";
                res.data = r;
                res.code = StatusCode.Ok;
            }
            else
            {
                res.msg = "修改失败";
                res.data = r;
                res.code = StatusCode.Fail;
            }
            return res;
        }
    }
}
